package userlogic

import (
	"context"
	"fmt"

	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/dao/model"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/login_rpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type CheckUserPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCheckUserPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckUserPasswordLogic {
	return &CheckUserPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CheckUserPasswordLogic) CheckUserPassword(in *login_rpc.CheckUserPasswordReq) (*login_rpc.CheckUserPasswordResp, error) {
	user, err := model.NewUserModel(l.ctx, l.svcCtx.Db).GetUserByUserId(in.UserId)
	if err != nil {
		return nil, fmt.Errorf("get user error: %v", err)
	}

	if HashPassword(in.Password) != user.Password {
		return nil, fmt.Errorf("wrong account or password")
	}

	return &login_rpc.CheckUserPasswordResp{
		CommonResp: &login_rpc.CommonResp{},
	}, nil
}
